﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Gma.UserActivityMonitor;

namespace MK_I
{
    public partial class Form1 : Form
    {
        int x = 0;
        int y = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PositionText.Text = string.Format("x={0:0000}; y={1:0000}", x, y);
        }

        private void HookManager_MouseMove(object sender, MouseEventArgs e)
        {
            PositionText.Text = string.Format("x={0:0000}; y={1:0000}", e.X, e.Y);
        }
        private void HookManager_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PositionText.Text = string.Format("x={0:0000}; y={1:0000}", e.X, e.Y);
                HookManager.MouseClick -= HookManager_MouseClick;
                HookManager.MouseMove -= HookManager_MouseMove;
                x = e.X;
                y = e.Y;
            }
        }
        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);
        [DllImport("User32")]
        public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        [Flags]
        public enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        private void getPosition_Click(object sender, EventArgs e)
        {
            HookManager.MouseMove += HookManager_MouseMove;
            HookManager.MouseClick += HookManager_MouseClick;
        }

        private void openItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Insert(0, "開寶開始");
            listBox1.Invalidate();
            listBox1.Update();
            int loop = 0;
            int.TryParse(LoopTxt.Text, out loop);
            while (loop > 0)
            {
                SetCursorPos(x, y);
                mouse_event(MouseEventFlag.RightDown | MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
                listBox1.Items.Insert(0, "右鍵");
                listBox1.Invalidate();
                listBox1.Update();

                System.Threading.Thread.Sleep(500);
                mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
                listBox1.Items.Insert(0, "左鍵");
                listBox1.Invalidate();
                listBox1.Update();
                System.Threading.Thread.Sleep(4000);
                listBox1.Items.Insert(0, "等待4秒");
                listBox1.Invalidate();
                listBox1.Update();
                mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
                listBox1.Items.Insert(0, "左鍵");
                listBox1.Invalidate();
                listBox1.Update();
                System.Threading.Thread.Sleep(500);
                loop--;
            }
            listBox1.Items.Insert(0, "聖誕包完成");
            listBox1.Invalidate();
            listBox1.Update();
        }
    }
}
